package com.mao.flink;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mao.pojo.Root;
import com.mao.pojo.Target;
import org.apache.poi.openxml4j.util.ZipSecureFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.mao.utils.ExcelUtils.writeIntoExcel;


/**
 * @Description:
 * @Param:
 * @Thought:
 * @Author: Wenjie Mao
 * @create： 2022-11-23 18:23
 */
public class JsonToBean {
    public static void main(String[] args) throws Exception {
        castAllFileInFolder("E:\\Android\\cloud-computing-nju2022\\java\\maoa\\json\\all\\","d://giteeCount.xlsx");
    }


    /*
    @Param: file->正在处理的当前文件
    @Param: destUrl ->保存到的表格文件位置

     */
    public static void castJsonToExcel(File file, String destUrl) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();

        InputStream inputStream =  new FileInputStream(file);

        ZipSecureFile.setMinInflateRatio(-1.0d);

        byte[] bytes = new byte[1024];

        stringBuilder.append("{\"list\":");

        while(true){
            int read = inputStream.read(bytes);
            if(read == -1)break;
            else stringBuilder.append(new String(bytes,0,read, StandardCharsets.UTF_8));
        }
        stringBuilder.append("}");
        String s = stringBuilder.toString();
        List<Target> jsonAsTargetList = getJsonAsTargetList(s);
        writeIntoExcel(destUrl,jsonAsTargetList);
    }

    //将json数组文件中提取出每个json所需的target，返回list
    public static List<Target> getJsonAsTargetList(String jsonString){
        JSONObject json = JSON.parseObject(jsonString);
        JSONArray list = json.getJSONArray("list");
        ArrayList<Target> targetList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonObject = list.getJSONObject(i);
            Root root = JSON.toJavaObject(jsonObject, Root.class);

            Target target = new Target();
            target.setId(root.getId());
            target.setLanguage(root.getLanguage());
            target.setFull_name(root.getFull_name());
            target.setForks_count(root.getForks_count());
            target.setStargazers_count(root.getStargazers_count());
            target.setWatchers_count(root.getWatchers_count());
            target.setOpen_issues_count(root.getOpen_issues_count());
            target.setHas_issues(root.isHas_issues());
            target.setHas_wiki(root.isHas_wiki());

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String format = simpleDateFormat.format(root.getCreated_at());

            target.setCreate_at(format);

            targetList.add(target);
        }
        return targetList;
    }

    /*
    @Param: folder->文件夹
     */
    public static void castAllFileInFolder(String folder, String destUrl) throws IOException {
        File file = new File(folder);
        File[] files = file.listFiles();
        for (File tempFile : files) {
            System.out.println("processing "+tempFile.getName());
            castJsonToExcel(tempFile,destUrl);
        }
    }

}
